package com.wx.service.demo.ui;

import android.os.Bundle;
import android.os.RemoteException;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Spinner;
import android.widget.SpinnerAdapter;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.wx.service.demo.R;
import com.wx.service.demo.service.WxServiceUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import wx.IWxManager;

public class MainActivity extends AppCompatActivity {
    private static String TAG = "MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        IWxManager manager = WxServiceUtils.getService();
        try {
            initUserInfoViewLayout(manager.getUserInfo());
        } catch (JSONException | RemoteException e) {
            e.printStackTrace();
        }
    }

    private void initUserInfoViewLayout(String userInfo) throws JSONException {
        Log.i(TAG, "userInfo:" + userInfo);
        JSONObject json = new JSONObject(userInfo);
        JSONArray usersArray = json.getJSONArray("users");
        Spinner spinner = findViewById(R.id.users_spinner);
        spinner.setAdapter(new BaseAdapter() {
            @Override
            public int getCount() {
                return usersArray.length();
            }

            @Override
            public Object getItem(int position) {
                try {
                    return usersArray.getJSONObject(position);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                return "错误";
            }

            @Override
            public long getItemId(int position) {
                return position;
            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                TextView textView = new TextView(MainActivity.this);
                textView.setTextSize(20);
                JSONObject jsonObject = (JSONObject) getItem(position);
                try {
                    textView.setText(jsonObject.getString("name"));
                } catch (JSONException e) {
                    textView.setText("未知");
                    e.printStackTrace();
                }
                return textView;
            }
        });

        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                try {
                    JSONObject jsonObject = (JSONObject) usersArray.getJSONObject(position);
                    updateUserInfo(jsonObject);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        spinner.invalidate();
    }

    private void updateUserInfo(JSONObject json) throws JSONException {
        TextView nameTextView = findViewById(R.id.user_name_text_view);
        nameTextView.setText(json.getString("name"));
    }
}